import { defineConfig, loadEnv } from "vite";
import path from 'path'
import uni from "@dcloudio/vite-plugin-uni";
import TransformPages from "./src/routerPlugins";
const tfPages = new TransformPages({
	includes: ['path', 'name', 'aliasPath', 'meta']
})
// let NODE_ENV = "development";
// if (process.env.NODE_ENV === 'development') {
// 	console.log('开发环境')
// }else{
// 	console.log('生产环境')
// 	NODE_ENV = "product";
// }
// 跨域代理重写
const regExps = (value: string, reg: string): string => {
	return value.replace(new RegExp(reg, "g"), "");
};
export default ({ mode }) => {
	process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
	return defineConfig({
		base: process.env.VITE_PUBLIC_PATH,
		envDir: "./",
		define: {
			ROUTER: JSON.stringify(tfPages.routes),
		},
		resolve: {
			// 设置路径别名
			alias: {
				// '': path.resolve(__dirname, './'),
				'@': path.resolve(__dirname, './src'),
				// '@components': path.resolve(__dirname, './src/components'),  
				// '@views': path.resolve(__dirname, './src/views')  
			}
		},
		plugins: [uni()],
		server: {
			port: parseInt(process.env.VITE_PORT),
			host: "0.0.0.0",

			open: false,
			https: false,
			cors: true,
			proxy: process.env.VITE_PROXY_DOMAIN_REAL.length > 0 ? {
				[process.env.VITE_PROXY_DOMAIN]: {
					target: process.env.VITE_PROXY_DOMAIN_REAL,
					// ws: true,
					changeOrigin: true,
					rewrite: (path: string) => regExps(path, process.env.VITE_PROXY_DOMAIN)
				  }
			} : null
		}
	})
}